package com.ydgk.jishuzhan.controller;


import com.ydgk.jishuzhan.beans.AjaxResult;
import com.ydgk.jishuzhan.beans.Comment;
import com.ydgk.jishuzhan.beans.Score;
import com.ydgk.jishuzhan.dao.UserMapper;
import com.ydgk.jishuzhan.service.CommentService;
import com.ydgk.jishuzhan.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.Calendar;


@RequestMapping("/comment")
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ScoreService scoreService;

    @ResponseBody
    @RequestMapping("/doAjaxComment")
    public AjaxResult doAjaxComment(Comment comment){
        AjaxResult ajaxResult = new AjaxResult();

        try{
            Date date = new Date(System.currentTimeMillis());
            comment.setSendTime(date);
            comment.getCommentInfo().setUser(userMapper.findUserById(comment.getUserId()));
            // 进行插入操作
            commentService.insert(comment);
            //添加积分
            scoreService.scoreAdd(new Score(null,"积分增加","编写评论",3,date,comment.getUserId()));
            ajaxResult.setSuccess(true);
            ajaxResult.setData(comment);
        } catch (Exception e){
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/doAjaxComment2")
    public AjaxResult doAjaxComment2(Comment comment){
        AjaxResult ajaxResult = new AjaxResult();

        try{
            Date date = new Date(System.currentTimeMillis());
            comment.setSendTime(date);
            comment.getCommentInfo().setUser(userMapper.findUserById(comment.getUserId()));
            // 进行插入操作
            commentService.insert2(comment);
            //添加积分
            scoreService.scoreAdd(new Score(null,"积分增加","编写答案",3,date,comment.getUserId()));
            ajaxResult.setData(comment);
            ajaxResult.setSuccess(true);
        } catch (Exception e){
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
